package cn.youyeku.zsxh.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.youyeku.zsxh.R;
import cn.youyeku.zsxh.Utils.CircleImageView;
import cn.youyeku.zsxh.Utils.ImageUtils;
import cn.youyeku.zsxh.Utils.LogUtils;
import cn.youyeku.zsxh.adapter.SimpleAdapter;
import cn.youyeku.zsxh.api.XhApi;
import cn.youyeku.zsxh.bean.Fans_Follows;

import cn.youyeku.zsxh.ui.UIControl;
import cn.youyeku.zsxh.ui.activity.ABS.HandleActivity;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by youye on 17-4-2.
 */

public  class FollowsActivity extends HandleActivity {


    protected List<Fans_Follows> mData =new ArrayList<>();
    protected SimpleAdapter mAdapter;
    private RecyclerView mRecyCommon;

    LinearLayoutManager layoutManager;
    private String bundle_title;

    @Override
    protected void onChange() {

    }

    @Override
    protected void onError() {

    }

    @Override
    protected void onSuccess() {
        mAdapter.add(mData);


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.act_recycle);
        initView();

        if (savedInstanceState != null) {
// Restore value of members from saved state
            bundle_title = savedInstanceState.getString("bundle_title");
        } else {
            Intent intent = getIntent();
            Bundle bundle = intent.getExtras();
            bundle_title = bundle.getString("title");
        }

        initActionBar();
        initData();

    }

    private void initData() {
        mActionBar.setTitle(bundle_title);
        setLayoutV_H(LinearLayoutManager.VERTICAL);

        mAdapter = new SimpleAdapter<Fans_Follows>(R.layout.list_item) {
            @Override
            protected void setmOnItemClick(View item, final Fans_Follows data) {
                item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UIControl.showUserInfo(getApplicationContext(),data.getUid());
                    }
                });

            }

            @Override
            protected void onBindData(ListHolder listHolder, View item, Fans_Follows data) {
               CircleImageView img = listHolder.getChildView(R.id.recycler_item_image);
                TextView nickName = listHolder.getChildView(R.id.recycler_item_text);

                ImageUtils.GlideCircleImg(getApplicationContext(),img, XhApi.getFace(data.getUid()));
                nickName.setText(data.getNick_name());

            }
        };
        mRecyCommon.setAdapter(mAdapter);


        XhApi.get_friends(bundle_title,new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();
                LogUtils.w("获取回应" + data);
                try {
                    JSONObject object = new JSONObject(data);
                    int code = object.getInt("code");

                    LogUtils.w("code  == " + code + object.getString("message"));
                    if(code==200){
                        Gson gson = new Gson();
                        mData = gson.fromJson(object.getString("data"), new TypeToken<List<Fans_Follows>>() {
                        }.getType());
                    }
                    Message msg = new Message();
                    msg.what = code;
                    handler.sendMessage(msg);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * @param type 设置水平方向还是垂直布局
     */
    public void setLayoutV_H(int type) {
        layoutManager = new LinearLayoutManager(getApplicationContext());
        layoutManager.setOrientation(type);
        mRecyCommon.setLayoutManager(layoutManager);
    }


    private void initView() {
        mRecyCommon = (RecyclerView) findViewById(R.id.recy_common);
    }
}
